package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;
import java.text.ParseException;

/**
 * 有效期更改交易明细表实体类 作者: 日期：2018-08-12 17:14:44 描述：
 */
public class TradeChangeValid extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 主表ID
	 */
	private String commId;

	/*
	 * 原有效开始时间
	 */
	private String beginTimeBefore;

	/*
	 * 原有效结束时间
	 */
	private String endTimeBefore;

	/*
	 * 新有效开始时间
	 */
	private String beginTimeAfter;

	/*
	 * 新有效结束时间
	 */
	private String endTimeAfter;

	/**
	 * 默认构造方法
	 **/
	public TradeChangeValid() {
		super();
	}

	/**
	 * getger和setter方法
	 */

	public String getCommId() {
		return commId;
	}

	public void setCommId(String commId) {
		this.commId = commId;
	}

	public String getBeginTimeBefore() {
		return beginTimeBefore;
	}

	public void setBeginTimeBefore(String beginTimeBefore) throws ParseException {
		checkTimeStyle(beginTimeBefore, "BeginTimeBefore");
		this.beginTimeBefore = beginTimeBefore;
	}

	public String getEndTimeBefore() {
		return endTimeBefore;
	}

	public void setEndTimeBefore(String endTimeBefore) throws ParseException {
		checkTimeStyle(endTimeBefore, "EndTimeBefore");
		this.endTimeBefore = endTimeBefore;
	}

	public String getBeginTimeAfter() {
		return beginTimeAfter;
	}

	public void setBeginTimeAfter(String beginTimeAfter) throws ParseException {
		checkTimeStyle(beginTimeAfter, "BeginTimeAfter");
		this.beginTimeAfter = beginTimeAfter;
	}

	public String getEndTimeAfter() {
		return endTimeAfter;
	}

	public void setEndTimeAfter(String endTimeAfter) throws ParseException {
		checkTimeStyle(endTimeAfter, "EndTimeAfter");
		this.endTimeAfter = endTimeAfter;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeChangeValid init(String message) throws Exception {

		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {


			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 14, 10, 8, 8, 8, 8, 5, 10, 14, 14, 14, 14, 20, 10, 2, 2, 16, 1, 1,
					2, 2, 2, 4, 4, 1, 4, 5, -1, 5 };

			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setCost(Long.parseLong(messageParser.getFiled(11))); // 费用
			this.setBalance(Long.parseLong(messageParser.getFiled(12)));
			this.setCardSeq(Long.parseLong(messageParser.getFiled(13)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(14)));

			this.setBeginTimeBefore(messageParser.getFiled(15));
			this.setEndTimeBefore(messageParser.getFiled(16));
			this.setBeginTimeAfter(messageParser.getFiled(17));
			this.setEndTimeAfter(messageParser.getFiled(18));

			this.setSamcardId(messageParser.getFiled(19));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(20)));
			this.setCardCode(messageParser.getFiled(21));
			this.setTradeSub(messageParser.getFiled(22));
			this.setPhysicalId(messageParser.getFiled(23));

			this.setTestFlag(messageParser.getFiled(24));
			this.setGreyFlag(messageParser.getFiled(25));
			this.setMedia(messageParser.getFiled(26));
			this.setCardVersion(messageParser.getFiled(27));
			this.setSubCardCode(messageParser.getFiled(28));

			this.setCityCode(messageParser.getFiled(29));
			this.setBusinessCode(messageParser.getFiled(30));

			this.setIndustrySplit(messageParser.getFiled(31));
			this.setIssuer(messageParser.getFiled(32));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(33)));
			super.setIndustryData(messageParser.getFiled(34));
			super.setCrc16(messageParser.getFiled(35));

		} else if(message.startsWith("SZMETROV3")) {

			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
			 付费方式
			 支付方式
			 发卡方机构码
			 发卡方交易流水号
			 运营模式
			 三、票卡发行方标识	从长度4变为长度2
			 */
			int[] splitNumber = {
					9, 8, 20, 2, 10,   9, 6, 14, 10, 8,   8, 8, 8, 5, 10,   14, 14, 14, 14, 20,
					10, 2, 2, 16, 1,   1, 2, 2, 2, 4,   4,
					1, 2, 8, 10, 4,    //V3增加的字段
					1, 2, 5,
					-1, 5};
			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setTradeTime(messageParser.getFiled(7));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(8)));
			this.setTradeMoney(Long.parseLong(messageParser.getFiled(9)));

			this.setTradeValue(Long.parseLong(messageParser.getFiled(10)));
			this.setCost(Long.parseLong(messageParser.getFiled(11))); // 费用
			this.setBalance(Long.parseLong(messageParser.getFiled(12)));
			this.setCardSeq(Long.parseLong(messageParser.getFiled(13)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(14)));

			this.setBeginTimeBefore(messageParser.getFiled(15));
			this.setEndTimeBefore(messageParser.getFiled(16));
			this.setBeginTimeAfter(messageParser.getFiled(17));
			this.setEndTimeAfter(messageParser.getFiled(18));

			this.setSamcardId(messageParser.getFiled(19));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(20)));
			this.setCardCode(messageParser.getFiled(21));
			this.setTradeSub(messageParser.getFiled(22));
			this.setPhysicalId(messageParser.getFiled(23));

			this.setTestFlag(messageParser.getFiled(24));
			this.setGreyFlag(messageParser.getFiled(25));
			this.setMedia(messageParser.getFiled(26));
			this.setCardVersion(messageParser.getFiled(27));
			this.setSubCardCode(messageParser.getFiled(28));

			this.setCityCode(messageParser.getFiled(29));
			this.setBusinessCode(messageParser.getFiled(30));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(31));
			this.setPayType(messageParser.getFiled(32));
			this.setIssuerCode(messageParser.getFiled(33));
			this.setIssuerSeq(messageParser.getFiled(34));
			this.setOperMode(messageParser.getFiled(35));

			this.setIndustrySplit(messageParser.getFiled(36));
			this.setIssuer(messageParser.getFiled(37));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(38)));
			super.setIndustryData(messageParser.getFiled(39));
			super.setCrc16(messageParser.getFiled(40));
		}

		return this;
	}
}